package com.estore.view;

import com.estore.model.Product;
import com.estore.service.ProductService;
import java.util.Scanner;

public class AdminConsole {
    private final ProductService productService = new ProductService();
    private final Scanner scanner = new Scanner(System.in);
    private static final String ADMIN_PASSWORD = "123"; // 管理员密码

    // 管理员入口
    public void start() {
        if (!login()) {
            System.out.println("管理员登录失败");
            return;
        }
        adminMenu();
    }

    // 登录验证
    private boolean login() {
        System.out.print("请输入管理员密码: ");
        String password = scanner.nextLine();
        return ADMIN_PASSWORD.equals(password);
    }

    // 管理员菜单
    private void adminMenu() {
        while (true) {
            System.out.println("\n--- 管理员系统 ---");
            System.out.println("1. 添加商品");
            System.out.println("2. 修改商品");
            System.out.println("3. 删除商品");
            System.out.println("4. 查看所有商品");
            System.out.println("5. 退出管理员模式");
            System.out.print("请选择操作: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消费换行符

            switch (choice) {
                case 1:
                    addProduct();
                    break;
                case 2:
                    updateProduct();
                    break;
                case 3:
                    deleteProduct();
                    break;
                case 4:
                    showAllProducts();
                    break;
                case 5:
                    System.out.println("退出管理员模式");
                    return;
                default:
                    System.out.println("无效选项，请重新输入");
            }
        }
    }

    // 添加商品
    private void addProduct() {
        System.out.print("请输入商品ID: ");
        String id = scanner.nextLine();
        if (productService.findProductById(id) != null) {
            System.out.println("该商品ID已存在");
            return;
        }

        System.out.print("请输入商品名称: ");
        String name = scanner.nextLine();
        System.out.print("请输入商品价格: ");
        double price = scanner.nextDouble();
        System.out.print("请输入商品库存: ");
        int stock = scanner.nextInt();
        scanner.nextLine();

        productService.addProduct(new Product(id, name, price, stock));
        System.out.println("商品添加成功");
        productService.saveData(); // 保存数据
    }

    // 修改商品
    private void updateProduct() {
        System.out.print("请输入要修改的商品ID: ");
        String id = scanner.nextLine();
        Product product = productService.findProductById(id);
        if (product == null) {
            System.out.println("商品不存在");
            return;
        }

        System.out.print("请输入新名称(" + product.getName() + "): ");
        String name = scanner.nextLine();
        if (!name.isEmpty()) product.setName(name);

        System.out.print("请输入新价格(" + product.getPrice() + "): ");
        String priceStr = scanner.nextLine();
        if (!priceStr.isEmpty()) {
            product.setPrice(Double.parseDouble(priceStr));
        }

        System.out.print("请输入新库存(" + product.getStock() + "): ");
        String stockStr = scanner.nextLine();
        if (!stockStr.isEmpty()) {
            product.setStock(Integer.parseInt(stockStr));
        }

        System.out.println("商品修改成功");
        productService.saveData(); // 保存数据
    }

    // 删除商品
    private void deleteProduct() {
        System.out.print("请输入要删除的商品ID: ");
        String id = scanner.nextLine();
        if (productService.deleteProduct(id)) {
            System.out.println("商品删除成功");
            productService.saveData(); // 保存数据
        } else {
            System.out.println("商品不存在");
        }
    }

    // 查看所有商品
    private void showAllProducts() {
        System.out.println("\n--- 所有商品 ---");
        productService.getAllProducts().forEach(System.out::println);
    }
}
